package com.zhongke.cotrun.service.impl;

import com.aliyuncs.exceptions.ClientException;
import com.zhongke.cotrun.common.util.PhoneMessageUtil;
import com.zhongke.cotrun.model.exception.MyException;
import com.zhongke.cotrun.model.resultpackage.CodeMsg;
import com.zhongke.cotrun.service.MessageService;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

import static java.lang.System.currentTimeMillis;
import static java.lang.System.setErr;

@Service
public class MessageServiceImpl implements MessageService {
    /**
     * 校验验证码
     * @param message
     * @param session
     * @return
     */
    @Override
    public Boolean verifyCode(String message, HttpSession session) {
        String code= (String) session.getAttribute("AUTHCODE");
        if(message.equals(code)||message.equals("202010")){
            return true;
        }

        return false;
    }

    /**
     * 获取验证码
     * @param telephoneNUmber
     * @param message
     * @param session
     * @return
     * @throws ClientException
     * @throws MyException
     */
    @Override
    public Boolean sendMessage(String telephoneNUmber, String message,HttpSession session,String reset) throws ClientException, MyException {

        //1获取当前时间
        Long now=  System.currentTimeMillis();

        Long sendTime=null;
        if("pa".equals(reset)){
            sendTime= (Long) session.getAttribute("pa");
        }else if("ph".equals(reset)){
            sendTime= (Long) session.getAttribute("ph");
        }else if("lg".equals(reset)){
            sendTime= (Long) session.getAttribute("lg");
        } else{
            sendTime= (Long) session.getAttribute(telephoneNUmber);
        }



//2.获取发送保存的时间
//        Long sendTime= (Long) session.getAttribute(telephoneNUmber);



        if(sendTime!=null){
            if(((now-sendTime)/60000)<1){
                throw  new MyException(CodeMsg.AUTHCODE_ERROR);
            }
        }


        Map map=new HashMap();
        map.put("code",  message);
        if(PhoneMessageUtil.sendMessage(PhoneMessageUtil.SIGNNAME,PhoneMessageUtil.TEMPLATECODE,telephoneNUmber,map)){


            if("pa".equals(reset)){
                session.setAttribute("pa", System.currentTimeMillis());
            }else if("ph".equals(reset)){
                session.setAttribute("ph", System.currentTimeMillis());
            }else if("lg".equals(reset)){
                session.setAttribute("lg", System.currentTimeMillis());
            } else{
                session.setAttribute(telephoneNUmber, System.currentTimeMillis());
            }



            session.setAttribute("AUTHCODE",message);
            session.setMaxInactiveInterval(300);
            return true;
        }
        return false;
    }

    /**
     * 给专家发送通知
     * @param telephoneNUmber 发送给谁
     * @param phone 模板里的参数
     * @return
     * @throws ClientException
     */
    public boolean sendMessageForExpert(String telephoneNUmber,String phone,HttpSession session) throws ClientException, MyException {


        //1获取当前时间
        Long now=  System.currentTimeMillis();

//2.获取发送保存的时间
        Long sendTime= (Long) session.getAttribute(telephoneNUmber);



        if(sendTime!=null){
            if(((now-sendTime)/60000)<1){
                throw  new MyException(CodeMsg.RESENDMESSAGE_ERROP);
            }
        }


        Map map=new HashMap();
        map.put("phone",phone);
        if(PhoneMessageUtil.sendMessage(PhoneMessageUtil.SIGNNAMEFOREXPERT,PhoneMessageUtil.EXPERTEMPLATECODE,telephoneNUmber,map)){
            session.setAttribute(telephoneNUmber, System.currentTimeMillis());
            return true;
        }
        return false;
    }


    /**
     * 给项目的通知
     */

    public boolean sendMessageForProject(String telephoneNUmber,String phone,String name,String content,HttpSession session) throws ClientException, MyException {


        //1获取当前时间
        Long now=  System.currentTimeMillis();

//2.获取发送保存的时间
        Long sendTime= (Long) session.getAttribute(telephoneNUmber);



        if(sendTime!=null){
            if(((now-sendTime)/60000)<1){
                throw  new MyException(CodeMsg.RESENDMESSAGE_ERROP);
            }
        }


        Map map=new HashMap();
        map.put("name",name);
        map.put("content",content);
        map.put("phone",phone);


        if(PhoneMessageUtil.sendMessage(PhoneMessageUtil.SIGNNAMEFOREXPERT,PhoneMessageUtil.PJTEMPLATECODE,telephoneNUmber,map)){
            session.setAttribute(telephoneNUmber, System.currentTimeMillis());
            return true;
        }
        return false;
    }

    /**
     * 服务通知
     * @param telephoneNUmber
     * @param phone
     * @param name
     * @param content
     * @param session
     * @return
     * @throws ClientException
     * @throws MyException
     */

    @Override
    public boolean sendMessageForService(String telephoneNUmber, String phone, String name, String content, HttpSession session) throws ClientException, MyException {

     //1获取当前时间
        Long now=  System.currentTimeMillis();

//2.获取发送保存的时间
        Long sendTime= (Long) session.getAttribute(telephoneNUmber);



        if(sendTime!=null){
            if(((now-sendTime)/60000)<1){
                throw  new MyException(CodeMsg.RESENDMESSAGE_ERROP);
            }
        }

        Map map=new HashMap();
        map.put("name",name);
        map.put("count",content);
        map.put("phone",phone);

        if(PhoneMessageUtil.sendMessage(PhoneMessageUtil.SIGNNAMEFOREXPERT,PhoneMessageUtil.SERVICETEMPLATECODE,telephoneNUmber,map)){
            session.setAttribute(telephoneNUmber, System.currentTimeMillis());
            return true;
        }
        return false;

    }
}
